let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tar: ['报价/接单', '待完成', '已完成'],
    masterId:null,
    currentIndex: 0,
    nodata: false,
    state: false,
    orderInfo: [],
    i: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {    
    var app = getApp();
    // 读取全局数据
    var id = app.globalData.userID;
    // this.getDataList();
    // console.log("========" + id);
    if (id == null || id == '') {
      this.checkLogin()
    }
    this.getData()
  },

  // 获取订单信息
  getData() {
    let statusFilter = "";
    if (this.data.currentIndex === 0) {
      statusFilter = "orderStatus=0";
      this.setData({
        masterId:null
      })
      wx.request({
        url: `http://localhost:8080/usermy/order/listAll?${statusFilter}`,
        method: "GET",
        success: (res) => {
          this.setData({
            orderInfo: res.data.data
          })
          console.log(this.data.orderInfo);
          // this.getData1();
        }
      })
    } else if (this.data.currentIndex === 1) {
      statusFilter = "&orderStatus=1";
      this.setData({
        masterId:app.globalData.userID
      })
      // console.log(this.data.masterId);
      wx.request({
        url: `http://localhost:8080/usermy/order/listAll?masterId=${this.data.masterId}${statusFilter}`,
        method: "GET",
        success: (res) => {
          this.setData({
            orderInfo: res.data.data
          })
          console.log(this.data.orderInfo);
          // this.getData1();
        }
      })
    } else if (this.data.currentIndex === 2) {
      statusFilter = "&orderStatus=2";
      this.setData({
        masterId:app.globalData.userID
      })
      wx.request({
        url: `http://localhost:8080/usermy/order/listAll?masterId=${this.data.masterId}${statusFilter}`,
        method: "GET",
        success: (res) => {
          this.setData({
            orderInfo: res.data.data
          })
          console.log(this.data.orderInfo);
          // this.getData1();
        }
      })
    }
  },

  // 跳转登录
  checkLogin: function () {
    wx.showModal({
      title: '未登录',
      content: '请先登录',
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/packageMy/pages/login/login'
          });
        }
      }
    });
  },

  // 点击事件
  onPullDown: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log("当前页面id为" + index);
    this.setData({
      currentIndex: index,
    });
    this.getData(); // 根据新的currentIndex重新获取数据
  },

  pj: function () {
    this.setData({
      state: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  // 用户抢单
  rob(e) {
    console.log(e.target.dataset.id);
    wx.showModal({
      title: '提示',
      content: '确定要抢单么？',
      success: (res) => {
        if (res.confirm) {
          console.log('点击确定');
          wx.request({
            url: 'http://localhost:8080/usermy/order',
            method: "PUT",
            data: {
              id: e.target.dataset.id,
              masterId:app.globalData.userID,
              orderStatus: 1
            },
            success: () => {
              this.getData(); // 更新订单列表
            }
          })
        } else if (res.cancel) {
          console.log('点击取消');
        }
      }
    });
  },

  // 用户完成订单
  robover(e) {
    console.log(e.target.dataset.id);
    wx.showModal({
      title: '提示',
      content: '确定完成订单吗？',
      success: (res) => {
        if (res.confirm) {
          console.log('点击确定');
          wx.request({
            url: 'http://localhost:8080/usermy/order',
            method: "PUT",
            data: {
              id: e.target.dataset.id,
              masterId:app.globalData.userID,
              orderStatus: 2
            },
            success: () => {
              this.getData(); // 更新订单列表
            }
          })
        } else if (res.cancel) {
          console.log('点击取消');
        }
      }
    });
  },

  // 用户取消订单
  robcancel(e) {
    console.log(e.target.dataset.id);
    wx.showModal({
      title: '提示',
      content: '确定取消订单吗？',
      success: (res) => {
        if (res.confirm) {
          console.log('点击确定');
          wx.request({
            url: 'http://localhost:8080/usermy/order',
            method: "PUT",
            data: {
              id: e.target.dataset.id,
              masterId:app.globalData.userID,
              orderStatus: 0 // 假设3表示已取消
            },
            success: () => {
              this.getData(); // 更新订单列表
            }
          })
        } else if (res.cancel) {
          console.log('点击取消');
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
    // this.getDataList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});



